const DataStatisticSer = require('../../service/dataStatistics/DataStatisticSer')
const userSer = require('../../service/user/userSer')

class DataStatisticSerCon {
    //获取用户的访问ip
    async getUserIpLocation(ctx) {
        try {
            const ip = ctx.request.ip.split(':')[3]
            const location = ''
            const data = await DataStatisticSer.getUserIpLocation(ip, location)
            if (data.id) {
                ctx.body = {
                    code: '200'
                }
            }
        } catch (e) {
        }

    }

    async StatisticalSex(ctx) {
        const sexList = ['男', '女', '']
        try {
            const resData = []
            for (let i = 0; i < sexList.length; i++) {
                const data = await userSer.getUserListBySex(sexList[i])
                resData.push(data[0].count)
            }
            const sun = resData.reduce((a, b) => {
                return a + b
            })
            console.log(resData)
            ctx.body = {
                code: '200',
                data: [
                    (resData[0] / sun).toFixed(2) * 100,
                    (resData[1] / sun).toFixed(2) * 100,
                    (resData[2] / sun).toFixed(2) * 100,
                ]
            }
        } catch (e) {
            ctx.app.emit('error', e.toString(), ctx)

        }

    }

}

module.exports = new DataStatisticSerCon()
